package com.earthea.application.task.entity;

import cn.hutool.core.date.DateUtil;
import cn.hutool.system.SystemUtil;
import com.earthea.common.util.JacksonUtils;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Data
@NoArgsConstructor
public class Task {
    private String taskId;

    private String type;

    private String label;

    private String data;

    private int status = TaskStatus.INIT.getStatus();

    private int retry;

    private int maxRetry = TaskConstants.DEFAULT_MAX_RETRY;

    private String clientIp;

    private String result;

    private String createTime;

    private String updateTime;

    public Task(String type, String label, Object data) {
        this.type = type;
        this.label = label;
        this.data = JacksonUtils.toJson(data);
    }

    public Task(Object data) {
        this(TaskConstants.DEFAULT_TYPE, TaskConstants.DEFAULT_LABEL, data);
    }

    public static String getTableName() {
        return SystemUtil.get("earthea.application.task.table.name", TaskConstants.DEFAULT_TABLE_NAME);
    }

    public String getHistoryTableName() {
        String value = SystemUtil.get("earthea.application.task.history.table.name", TaskConstants.DEFAULT_HISTORY_TABLE_NAME);
        return DateUtil.parseLocalDateTime(createTime).format(DateTimeFormatter.ofPattern(value));
    }

    public static String getHistoryTableName(LocalDateTime localDateTime) {
        String value = SystemUtil.get("earthea.application.task.history.table.name", TaskConstants.DEFAULT_HISTORY_TABLE_NAME);
        return localDateTime.format(DateTimeFormatter.ofPattern(value));
    }
}
